<?php
/**
 * Created by PhpStorm.
 * User: krystal
 * Date: 2019/4/8
 * Time: 14:01
 */

namespace app\common\validate;

use think\Validate;

/**
 * 订单重构,新订单验证器
 * Class ZhunleOrders
 * @package app\common\validate
 */
class ZhunleOrder extends Validate
{
    protected $rule = [
        'trade_type' => 'require|number|in:1,2,5,6,8',
        'fortune_type' => 'require|number|in:1,2,3',
        'map_fortune' => 'require|number|in:1,2',
        'gid' => 'require|number|gt:0',
        'gctid' => 'require|number|gt:0',
        'gctids' => 'require|gt:0',
        'rid' => 'require|number|gt:0',
        'rids' => 'require|gt:0',
        'fortune_time' => 'require|gt:0|checkFortuneTime',
        'rec_start_time' => 'require|checkRecTime',
        'rec_end_time' => 'require|checkRecTime',
        'events' => 'require|checkEvents',
        'openid' => 'require',
        'order_id' => 'require|length:20,22'
    ];

    protected $message = [
        'trade_type.require' => '支付类型未知',
        'trade_type.number' => '支付类型错误',
        'trade_type.in' => '支付类型违规',
        'fortune_type.require' => '运势类型未知',
        'fortune_type.number' => '运势类型错误',
        'fortune_type.in' => '运势类型违规',
        'map_fortune.require' => '地图类型未知',
        'map_fortune.number' => '地图类型错误',
        'map_fortune.in' => '地图类型违规',
        'gid.require' => '商品类型未知',
        'gid.number' => '商品类型错误',
        'gid.gt' => '商品类型违规',
        'gctid.require' => '货品类型未知',
        'gctid.number' => '货品类型错误',
        'gctid.gt' => '货品类型违规',
        'gctids.require' => '货品类型未知',
        'gctids.gt' => '货品类型违规',
        'rid.require' => '档案类型未知',
        'rid.number' => '档案类型错误',
        'rid.gt' => '档案类型违规',
        'rids.require' => '档案类型未知',
        'rids.gt' => '档案类型违规',
        'fortune_time.require' => '开通时间格式未知',
        'fortune_time.gt' => '开通时间格式违规',
        'fortune_time.checkFortuneTime' => '开通时间格式违规',
        'rec_start_time.checkRecTime' => '校准生辰开始时间格式违规',
        'rec_start_time.require' => '校准生辰开始时间未知',
        'rec_end_time.checkRecTime' => '校准生辰结束时间格式违规',
        'rec_end_time.require' => '校准生辰结束时间未知',
        'events.checkEvents' => '校准生辰事件违规',
        'events.require' => '校准生辰事件未知',
        'openid.require' => 'openid未知',
        'order_id.require' => '缺少订单编号',
        'order_id.length' => '订单编号违规'
    ];

    protected $scene = [
        'v10_pay_rec' => [
            'trade_type',
            'gid',
            'gctid',
            'rid',
            'rec_start_time',
            'rec_end_time',
            'events'
        ],
        'v10_pay_fortune' => [
            'trade_type',
            'fortune_type',
            'gid',
            'gctid',
            'rids',
            'fortune_time',
        ],
        'v10_pay_map' => [
            'trade_type',
            'map_fortune',
            'gid',
            'gctids',
            'rid',
        ],
        'v10_pay_video' => [
            'trade_type',
            'gid',
            'gctid',
        ],
        'read' => [
            'order_id'
        ]
    ];

    protected $field = [
        'order_type' => '订单类型',
        'trade_type' => '支付类型',
        'fortune_type' => '运势类型',
        'map_fortune' => '地图类型',
        'gid' => '商品id',
        'gids' => '多个商品id',
        'rid' => '档案id',
        'rids' => '多个档案id',
        'fortune_time' => '开通运势时间',
        'rec_start_time' => '校准生辰开始时间',
        'rec_end_time' => '校准生辰结束时间',
        'events' => '校准生辰事件,json类型',
        'openid' => '网页微信支付参数'
    ];

    /**
     * 检查校准生辰的时间格式
     * @param $value
     * @param $rule
     * @param $data
     * @param $field
     * @return bool
     */
    public function checkRecTime($value, $rule, $data, $field)
    {
        $test_time = explode(':', $value);
        //判断下单时间格式是否正确,获取下单时间的时间戳
        if (count($test_time) !== 2) return false;
        $hour = $test_time[0];
        $minute = $test_time[1];

        if (strlen($hour) !== 2 || strlen($minute) !== 2 || $hour < 0 || $hour > 24 || $minute < 0 || $minute > 59) {
            return false;
        }
        return true;
    }

    /**
     * 检查校准生辰事件格式信息
     * @param $value
     * @param $rule
     * @param $data
     * @param $field
     * @return bool
     */
    public function checkEvents($value, $rule, $data, $field)
    {
        $events = json_decode($value, true);
        if (!empty($events)) {
            foreach ($events as $ev) {
                if (!isset($ev['desc']) || !isset($ev['type']) || $ev['type'] < 1 || $ev['type'] > 18 || !isset($ev['time'])) {
                    return false;
                }
                $test_time = explode('-', $ev['time']);
                //判断下单时间格式是否正确,获取下单时间的时间戳
                if (count($test_time) !== 3) return false;
                $year = $test_time[0];
                $month = $test_time[1];
                $day = $test_time[1];

                if (strlen($year) !== 4 || strlen($month) !== 2 || strlen($day) !== 2 || $month < 1 || $month > 12 || $day < 1 || $day > 31) {
                    return false;
                }
            }
        } else {
            return false;
        }

        return true;
    }

    /**
     * 检查运势开通的时间格式
     * @param $value //验证数据
     * @param $rule //验证规则
     * @param $data //全部数据,数组类型
     * @param $field //字段名,验证的字段名
     * @return bool
     */
    protected function checkFortuneTime($value, $rule, $data, $field)
    {
        $test_time = explode('-', $value);

        $fortune_type = $data['fortune_type'];

        //判断下单时间格式是否正确,获取下单时间的时间戳
        switch ($fortune_type) {
            case 1:
                if (count($test_time) !== 3) return false;
                $year = $test_time[0];
                $month = $test_time[1];
                $day = $test_time[2];

                if (strlen($year) !== 4 || !in_array(strlen($month), [1, 2]) || !in_array(strlen($day), [1, 2])) {
                    return false;
                }
                break;
            case 2:
                if (count($test_time) !== 2) return false;
                $year = $test_time[0];
                $month = $test_time[1];

                if (strlen($year) !== 4 || !in_array(strlen($month), [1, 2])) {
                    return false;
                }
                break;
            case 3:
                if (count($test_time) !== 1) return false;
                $year = $test_time[0];
                if (strlen($year) !== 4) {
                    return false;
                }
                break;
        }
        return true;
    }

}